﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AopFilters
{
    public static class DynamicAopProxy
    {
        public static T Create<T>(T t)
        {
            dynamic proxy = DispatchProxy.Create<T, AopProxy<T>>()!;
            proxy!.Instance = t;
            return proxy;
        }
    }

    public class AopProxy<T> : DispatchProxy
    {
        public T Instance = default;

        protected override object? Invoke(MethodInfo? targetMethod, object?[]? args)
        {
            BeforeAction(targetMethod);
            var result = targetMethod?.Invoke(Instance, args);
            AfterAction(targetMethod);
            return result;
        }

        public void BeforeAction(MethodInfo? targetMethod)
        {
            Console.WriteLine("方法执行前。。。");
        }

        public void AfterAction(MethodInfo? targetMethod)
        {
            Console.WriteLine("方法执行后。。。");
        }

    }
}
